import Cookie from 'js-cookie'
// import router from '../../router';
export default {
    state: {
        isCollapse: false,
        tabsList: [{
            path: '/home',
            name: 'home',
            label: '首页',
            icon: 'home'
        }],
        currentMenu: null,
        menu: []
    },

    mutations: {
        collapseMenu(state) {
            state.isCollapse = !state.isCollapse
        },
        selectMenu(state, val) {
            if (val.name !== 'home') {
                state.currentMenu = val;
                const result = state.tabsList.findIndex(item => item.name === val.name)
                if (result === -1) {
                    state.tabsList.push(val)
                }
            } else {
                state.currentMenu = null;
            }
        },
        closeTag(state, val) {
            const result = state.tabsList.findIndex(item => item.name === val.name)
            state.tabsList.splice(result, 1)
        },
        setMenu(state, val) {
            console.log(val);
            state.menu = val;
            console.log(JSON.stringify(val));
            Cookie.set('menu', JSON.stringify(val))
        },
        clearMenu(state) {
            state.menu = [];
            Cookie.remove('menu')
        },
        addMenu(state, router) {

            if (!Cookie.get('menu')) {
                console.log('增加失败');
                return
            }
            console.log('------------------------------已增加菜单');
            const menu = JSON.parse(Cookie.get('menu'))
                // console.log('addmenu', menu);
            state.menu = menu;
            const menuArray = [];
            menu.forEach(item => {
                if (item.children) {
                    item.children = item.children.map(item => {
                        item.component = () =>
                            import (`../views/${item.url}`)
                        return item
                    })
                    menuArray.push(...item.children)
                } else {
                    item.component = () =>
                        import (`../views/${item.url}`)
                    menuArray.push(item)
                }

            });
            //路由动态添加
            menuArray.forEach(item => {
                    // console.log(item);
                    router.addRoute('Main', item)
                })
                // console.log(router);
        }
    }
}